/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vue;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.File;
import java.util.Observable;
import java.util.Observer;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpringLayout;
import javax.swing.border.Border;
import modele.Environnement;

/**
 *
 * @author p1100274
 */
public class Fenetre extends JFrame implements Observer {

    public static final int CARRE = 0;
    public static final int TRIANGLE = 1;
    public static final int HEXA = 2;
    
    private int forme;
    
    private Environnement modele;

    private JMenu m;
    private JMenuBar menu;
    private JMenuItem ouvrir;
    private JMenuItem sauvegarder;
    private JMenuItem options;
    private JMenuItem quitter;
    private JPanel panelPrincipale;
    private JPanel panelGrille;
    private JPanel panelBoutons;
    private JPanel panelSuperPrincipale;
    private JButton start;
    private JButton pause;
    private JButton init;
    private JButton clean;
    private JButton placer;
    private int hauteurGrille;
    private int largeurGrille;
    private Case[][] tabCases;
    private JLabel iteration;
    private JPanel panelOptions;
    private JLabel temps;
    private JSpinner spinTemps;
    private JComboBox listeMotif;
    private JLabel labelMotif;

    public Fenetre(Environnement m, int h, int l) {
        super();
        
        forme = CARRE;
        modele = m;
        m.addObserver(this);
        hauteurGrille = h;
        largeurGrille = l;
        tabCases = new Case[largeurGrille][hauteurGrille];
        build();
        chargerMotif();
    }

    private void build() {
        menu = new JMenuBar();
        m = new JMenu("Fichier");
        ouvrir = new JMenuItem("Ouvrir...");
        sauvegarder = new JMenuItem("Sauvegarder...");
        options = new JMenuItem("Options...");
        quitter = new JMenuItem("Quitter");
        m.add(ouvrir);
        m.add(sauvegarder);
        m.add(options);
        m.addSeparator();
        m.add(quitter);
        menu.add(m);
        panelSuperPrincipale = new JPanel();
        panelPrincipale = new JPanel();
        panelGrille = new JPanel();
        panelBoutons = new JPanel();
        panelOptions = new JPanel();
        start = new JButton("Commencer");
        pause = new JButton("Pause");
        pause.setEnabled(false);
        init = new JButton("Initialiser");
        clean = new JButton("Effacer");
        placer = new JButton("Placer");

        iteration = new JLabel("Iteration:");
        temps = new JLabel("Intervalle (ms):");
        labelMotif = new JLabel("Chosir un motif:");

        spinTemps = new JSpinner();
        spinTemps.setModel(new javax.swing.SpinnerNumberModel(1000, 100, 5000, 100));

        listeMotif = new JComboBox();
        panelOptions.add(temps);
        panelOptions.add(spinTemps);
        panelOptions.add(labelMotif);
        panelOptions.add(listeMotif);
        panelOptions.add(placer);
        panelOptions.setLayout(new BoxLayout(panelOptions, BoxLayout.Y_AXIS));

        panelPrincipale.setLayout(new BorderLayout());
        panelGrille.setLayout(new GridLayout(hauteurGrille, largeurGrille));
        panelBoutons.setLayout(new BoxLayout(panelBoutons, BoxLayout.Y_AXIS));
        panelPrincipale.add(panelGrille, BorderLayout.CENTER);
        panelPrincipale.add(panelBoutons, BorderLayout.EAST);
        panelPrincipale.add(panelOptions, BorderLayout.SOUTH);

        setJMenuBar(menu);
        setTitle("Game of life");
        setSize(400, 400);

        initGrille();

        panelBoutons.add(init);
        panelBoutons.add(clean);
        panelBoutons.add(start);
        panelBoutons.add(pause);
        panelBoutons.add(iteration);

        this.add(panelPrincipale);

    }

    public int getCurrentForme(){
        return forme;
    }
    
    public Case[][] getCases() {
        return tabCases;
    }

    public int getHauteurGrille() {
        return hauteurGrille;
    }

    public int getLargeurGrille() {
        return largeurGrille;
    }

    public JButton getStart() {
        return start;
    }

    public JButton getPause() {
        return pause;
    }

    public JButton getInit() {
        return init;
    }

    public JButton getClean() {
        return clean;
    }

    public JMenuItem getOpen() {
        return ouvrir;
    }

    public JMenuItem getSave() {
        return sauvegarder;
    }

    public JMenuItem getQuit() {
        return quitter;
    }
    
    public JMenuItem getOptions(){
        return options;
    }

    public JSpinner getSpinTemps() {
        return spinTemps;
    }

    public JComboBox getListeMotif() {
        return listeMotif;
    }

    public JButton getPlacer() {
        return placer;
    }
    
    public void resizeGrille(Integer[] size) {
        hauteurGrille = size[1];
        largeurGrille = size[0];
        panelGrille.removeAll();
        panelPrincipale.remove(panelGrille);
        if(forme == CARRE)
        {
            panelGrille = new JPanel();
            panelGrille.setLayout(new GridLayout(hauteurGrille, largeurGrille));
        }
        else
        {
            panelGrille = new PanelGrille();
            panelGrille.setLayout(new SpringLayout());
            
        }
        panelPrincipale.add(panelGrille, BorderLayout.CENTER);
        tabCases = new Case[largeurGrille][hauteurGrille];
        initGrille();
        //le panel se met pas à jour sans ces deux lignes
        setSize(getSize().width+1, getSize().height);
        setSize(getSize().width-1, getSize().height);
        panelGrille.repaint();
    }

    private void initGrille() {
        Border blackline = BorderFactory.createLineBorder(Color.lightGray, 1);
        for (int i = 0; i < hauteurGrille; i++) {
            for (int y = 0; y < largeurGrille; y++) {
                if(forme == CARRE)
                {
                    tabCases[y][i] = new Case();
                    tabCases[y][i].setBorder(blackline);
                }
                else
                {
                    tabCases[y][i] = new CaseTriangle(((y+i)%2 == 0), largeurGrille, hauteurGrille);
                }
                
                panelGrille.add(tabCases[y][i]);
                
            }
            
        }
        setSize(getSize().width+1, getSize().height);
        setSize(getSize().width-1, getSize().height);
        panelGrille.repaint();
        
        
        
    }

    @Override
    public void update(Observable o, Object arg) {
        
        if (arg instanceof Boolean[][]) {
           
            Boolean[][] cases = (Boolean[][]) arg;
            for (int i = 0; i < largeurGrille; i++) {
                for (int y = 0; y < hauteurGrille; y++) {
                    
                    if (cases[i][y]) {
                       if(forme == CARRE)
                        tabCases[i][y].setBackground(Color.GREEN);
                       else
                       {
                         CaseTriangle t = (CaseTriangle) tabCases[i][y];
                         t.setColor(Color.GREEN);
                         t.repaint();
                       }
                        
                           
                    } else {
                       if(forme == CARRE)
                        tabCases[i][y].setBackground(Color.WHITE);
                       else
                       {
                         CaseTriangle t = (CaseTriangle) tabCases[i][y];
                         t.setColor(Color.WHITE);
                         t.repaint();
                       }
                    }
                }
                
            }
        } else if (arg instanceof Integer[]) {
            
            Integer size[] = (Integer[]) arg;
            resizeGrille(size);
            
        } else {
            int it = (int) arg;
            iteration.setText("Iteration:" + String.valueOf(it));
        }
    }

    public int getForme(){
        return forme;
    }
    
    public void setForme(int f){
        forme = f;
    }
    
    private void chargerMotif() {
        File rep = new File("motif");
        File[] list = rep.listFiles();
        for (int i = 0; i < list.length; i++) {
            String name = list[i].getName().replaceFirst(".jdlv", "");
            listeMotif.addItem(name);
        }
    }
    
    public void setButtonsDefault(){
        pause.setText("Pause");
        pause.setEnabled(false);
        start.setEnabled(true);
        clean.setEnabled(true);
        placer.setEnabled(true);
        placer.setText("Placer");
        init.setEnabled(true);
    }
    
    public void setModele(Environnement m){
        modele = m;
        m.addObserver(this);
    }
}
